/*
* @(#)GridBagLayout.java 1.24 98/07/01
*
* Copyright 1995-1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.awt;
import java.util.Hashtable;
import java.util.Vector;
class GridBagLayoutInfo implements java.io.Serializable {
int width, height; /* number of cells horizontally, vertically */
int startx, starty; /* starting point for layout */
int minWidth[]; /* largest minWidth in each column */
int minHeight[]; /* largest minHeight in each row */
double weightX[]; /* largest weight in each column */
double weightY[]; /* largest weight in each row */
GridBagLayoutInfo () {
minWidth = new int[GridBagLayout.MAXGRIDSIZE];
minHeight = new int[GridBagLayout.MAXGRIDSIZE];
weightX = new double[GridBagLayout.MAXGRIDSIZE];
weightY = new double[GridBagLayout.MAXGRIDSIZE];
}
}
/**
* The GridBagLayout
class is a flexible layout
* manager that aligns components vertically and horizontally,
* without requiring that the components be of the same size.
* Each GridBagLayout
object maintains a dynamic
* rectangular grid of cells, with each component occupying
* one or more cells, called its display area.
*
* Each component managed by a grid bag layout is associated
* with an instance of
* GridBagConstraints
* that specifies how the component is laid out within its display area.
*
* How a GridBagLayout
object places a set of components
* depends on the GridBagConstraints
object associated
* with each component, and on the minimum size
* and the preferred size of the components' containers.
*
* To use a grid bag layout effectively, you must customize one or more
* of the GridBagConstraints
objects that are associated
* with its components. You customize a GridBagConstraints
* object by setting one or more of its instance variables:
*
*
gridx
,
* gridy
* gridx = 0
,
* gridy = 0
.
* Use GridBagConstraints.RELATIVE
(the default value)
* to specify that the component be just placed
* just to the right of (for gridx
)
* or just below (for gridy
)
* the component that was added to the container
* just before this component was added.
* gridwidth
,
* gridheight
* gridwidth
)
* or column (for gridheight
)
* in the component's display area.
* The default value is 1.
* Use GridBagConstraints.REMAINDER
to specify
* that the component be the last one in its row (for gridwidth
)
* or column (for gridheight
).
* Use GridBagConstraints.RELATIVE
to specify
* that the component be the next to last one
* in its row (for gridwidth
)
* or column (for gridheight
).
* fill
* GridBagConstraints.NONE
(the default),
* GridBagConstraints.HORIZONTAL
* (make the component wide enough to fill its display area
* horizontally, but don't change its height),
* GridBagConstraints.VERTICAL
* (make the component tall enough to fill its display area
* vertically, but don't change its width), and
* GridBagConstraints.BOTH
* (make the component fill its display area entirely).
* ipadx
,
* ipady
* (ipadx * 2)
pixels (since the padding
* applies to both sides of the component). Similarly, the height of
* the component will be at least the minimum height plus
* (ipady * 2)
pixels.
* insets
* anchor
* GridBagConstraints.CENTER
(the default),
* GridBagConstraints.NORTH
,
* GridBagConstraints.NORTHEAST
,
* GridBagConstraints.EAST
,
* GridBagConstraints.SOUTHEAST
,
* GridBagConstraints.SOUTH
,
* GridBagConstraints.SOUTHWEST
,
* GridBagConstraints.WEST
, and
* GridBagConstraints.NORTHWEST
.
* weightx
,
* weighty
* weightx
) and column (weighty
),
* all the components clump together in the center of their container.
* This is because when the weight is zero (the default),
* the GridBagLayout
object puts any extra space
* between its grid of cells and the edges of the container.
* * The following figure shows ten components (all buttons) * managed by a grid bag layout: *
*
*
* Each of the ten components has the fill
field
* of its associated GridBagConstraints
object
* set to GridBagConstraints.BOTH
.
* In addition, the components have the following non-default constraints:
*
*
weightx = 1.0
* weightx = 1.0
,
* gridwidth = GridBagConstraints.REMAINDER
* gridwidth = GridBagConstraints.REMAINDER
* gridwidth = GridBagConstraints.RELATIVE
* gridwidth = GridBagConstraints.REMAINDER
* gridheight = 2
,
* weighty = 1.0
* gridwidth = GridBagConstraints.REMAINDER
* * Here is the code that implements the example shown above: *
*
* import java.awt.*; * import java.util.*; * import java.applet.Applet; * * public class GridBagEx1 extends Applet { * * protected void makebutton(String name, * GridBagLayout gridbag, * GridBagConstraints c) { * Button button = new Button(name); * gridbag.setConstraints(button, c); * add(button); * } * * public void init() { * GridBagLayout gridbag = new GridBagLayout(); * GridBagConstraints c = new GridBagConstraints(); * * setFont(new Font("Helvetica", Font.PLAIN, 14)); * setLayout(gridbag); * * c.fill = GridBagConstraints.BOTH; * c.weightx = 1.0; * makebutton("Button1", gridbag, c); * makebutton("Button2", gridbag, c); * makebutton("Button3", gridbag, c); * * c.gridwidth = GridBagConstraints.REMAINDER; //end row * makebutton("Button4", gridbag, c); * * c.weightx = 0.0; //reset to the default * makebutton("Button5", gridbag, c); //another row * * c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row * makebutton("Button6", gridbag, c); * * c.gridwidth = GridBagConstraints.REMAINDER; //end row * makebutton("Button7", gridbag, c); * * c.gridwidth = 1; //reset to the default * c.gridheight = 2; * c.weighty = 1.0; * makebutton("Button8", gridbag, c); * * c.weighty = 0.0; //reset to the default * c.gridwidth = GridBagConstraints.REMAINDER; //end row * c.gridheight = 1; //reset to the default * makebutton("Button9", gridbag, c); * makebutton("Button10", gridbag, c); * * setSize(300, 100); * } * * public static void main(String args[]) { * Frame f = new Frame("GridBag Layout Example"); * GridBagEx1 ex1 = new GridBagEx1(); * * ex1.init(); * * f.add("Center", ex1); * f.pack(); * f.setSize(f.getPreferredSize()); * f.show(); * } * } *
* @version 1.5, 16 Nov 1995
* @author Doug Stein
* @see java.awt.GridBagConstraints
* @since JDK1.0
*/
public class GridBagLayout implements LayoutManager2,
java.io.Serializable {
/**
* The maximum number of grid positions (both horizontally and
* vertically) that can be laid out by the grid bag layout.
* @since JDK1.0
*/
protected static final int MAXGRIDSIZE = 512;
/**
* The smallest grid that can be laid out by the grid bag layout.
* @since JDK1.0
*/
protected static final int MINSIZE = 1;
protected static final int PREFERREDSIZE = 2;
protected Hashtable comptable;
protected GridBagConstraints defaultConstraints;
protected GridBagLayoutInfo layoutInfo;
public int columnWidths[];
public int rowHeights[];
public double columnWeights[];
public double rowWeights[];
/**
* Creates a grid bag layout manager.
* @since JDK1.0
*/
public GridBagLayout () {
comptable = new Hashtable();
defaultConstraints = new GridBagConstraints();
}
/**
* Sets the constraints for the specified component in this layout.
* @param comp the component to be modified.
* @param constraints the constraints to be applied.
* @since JDK1.0
*/
public void setConstraints(Component comp, GridBagConstraints constraints) {
comptable.put(comp, constraints.clone());
}
/**
* Gets the constraints for the specified component. A copy of
* the actual GridBagConstraints
object is returned.
* @param comp the component to be queried.
* @return the constraint for the specified component in this
* grid bag layout; a copy of the actual constraint
* object is returned.
* @since JDK1.0
*/
public GridBagConstraints getConstraints(Component comp) {
GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
if (constraints == null) {
setConstraints(comp, defaultConstraints);
constraints = (GridBagConstraints)comptable.get(comp);
}
return (GridBagConstraints)constraints.clone();
}
/**
* Retrieves the constraints for the specified component.
* The return value is not a copy, but is the actual
* GridBagConstraints
object used by the layout mechanism.
* @param comp the component to be queried
* @return the contraints for the specified component.
* @since JDK1.0
*/
protected GridBagConstraints lookupConstraints(Component comp) {
GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
if (constraints == null) {
setConstraints(comp, defaultConstraints);
constraints = (GridBagConstraints)comptable.get(comp);
}
return constraints;
}
/**
* Determines the origin of the layout grid.
* Most applications do not call this method directly.
* @return the origin of the cell in the top-left
* corner of the layout grid.
* @since JDK1.1
*/
public Point getLayoutOrigin () {
Point origin = new Point(0,0);
if (layoutInfo != null) {
origin.x = layoutInfo.startx;
origin.y = layoutInfo.starty;
}
return origin;
}
/**
* Determines column widths and row heights for the layout grid.
*
* Most applications do not call this method directly. * @return an array of two arrays, containing the widths * of the layout columns and * the heights of the layout rows. * @since JDK1.1 */ public int [][] getLayoutDimensions () { if (layoutInfo == null) return new int[2][0]; int dim[][] = new int [2][]; dim[0] = new int[layoutInfo.width]; dim[1] = new int[layoutInfo.height]; System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width); System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height); return dim; } /** * Determines the weights of the layout grid's columns and rows. * Weights are used to calculate how much a given column or row * stretches beyond its preferred size, if the layout has extra * room to fill. *
* Most applications do not call this method directly.
* @return an array of two arrays, representing the
* horizontal weights of the layout columns
* and the vertical weights of the layout rows.
* @since JDK1.1
*/
public double [][] getLayoutWeights () {
if (layoutInfo == null)
return new double[2][0];
double weights[][] = new double [2][];
weights[0] = new double[layoutInfo.width];
weights[1] = new double[layoutInfo.height];
System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
return weights;
}
/**
* Determines which cell in the layout grid contains the point
* specified by (x, y)
. Each cell is identified
* by its column index (ranging from 0 to the number of columns
* minus 1) and its row index (ranging from 0 to the number of
* rows minus 1).
*
* If the
* Most applications do not call this method directly.
* @param comp the component to be removed.
* @see java.awt.Container#remove(java.awt.Component)
* @see java.awt.Container#removeAll()
* @since JDK1.0
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Determines the preferred size of the
* Most applications do not call this method directly.
* @param target the container in which to do the layout.
* @see java.awt.Container#getPreferredSize
* @since JDK1.0
*/
public Dimension preferredLayoutSize(Container parent) {
GridBagLayoutInfo info = GetLayoutInfo(parent, PREFERREDSIZE);
return GetMinSize(parent, info);
}
/**
* Determines the minimum size of the
* Most applications do not call this method directly.
* @param target the container in which to do the layout.
* @see java.awt.Container#doLayout
* @since JDK1.0
*/
public Dimension minimumLayoutSize(Container parent) {
GridBagLayoutInfo info = GetLayoutInfo(parent, MINSIZE);
return GetMinSize(parent, info);
}
/**
* Returns the maximum dimensions for this layout given the components
* in the specified target container.
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
* @see #preferredLayoutSize
*/
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
/**
* Returns the alignment along the x axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentX(Container parent) {
return 0.5f;
}
/**
* Returns the alignment along the y axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentY(Container parent) {
return 0.5f;
}
/**
* Invalidates the layout, indicating that if the layout manager
* has cached information it should be discarded.
*/
public void invalidateLayout(Container target) {
}
/**
* Lays out the specified container using this grid bag layout.
* This method reshapes components in the specified container in
* order to satisfy the contraints of this
* Most applications do not call this method directly.
* @param parent the container in which to do the layout.
* @see java.awt.Container
* @see java.awt.Container#doLayout
* @since JDK1.0
*/
public void layoutContainer(Container parent) {
ArrangeGrid(parent);
}
/**
* Returns a string representation of this grid bag layout's values.
* @return a string representation of this grid bag layout.
* @since JDK1.0
*/
public String toString() {
return getClass().getName();
}
/**
* Print the layout information. Useful for debugging.
*/
/* DEBUG
*
* protected void DumpLayoutInfo(GridBagLayoutInfo s) {
* int x;
*
* System.out.println("Col\tWidth\tWeight");
* for (x=0; x(x, y)
point lies
* outside the grid, the following rules are used.
* The column index is returned as zero if x
lies to the
* left of the layout, and as the number of columns if x
lies
* to the right of the layout. The row index is returned as zero
* if y
lies above the layout,
* and as the number of rows if y
lies
* below the layout.
* @param x the x coordinate of a point.
* @param y the y coordinate of a point.
* @return an ordered pair of indexes that indicate which cell
* in the layout grid contains the point
* (x, y).
* @since JDK1.1
*/
public Point location(int x, int y) {
Point loc = new Point(0,0);
int i, d;
if (layoutInfo == null)
return loc;
d = layoutInfo.startx;
for (i=0; itarget
* container using this grid bag layout.
* target
container
* using this grid bag layout.
* GridBagLayout
* object.
*